package admin

import (
	"duoke/model"
	"duoke/service"
	"duoke/tables"
	"duoke/utils/gconv"
	"duoke/utils/gtime"
	"duoke/utils/orm"
	"duoke/utils/response"
	"github.com/gin-gonic/gin"
	"time"
)

var Dashboard = new(dashboardCtl)

type dashboardCtl struct{}

func (ctl *dashboardCtl) Count(c *gin.Context) {
	accountR, _ := service.Request.Account(c)
	//判断子账户
	where := ""
	if accountR.Role == "goods" {
		where = "goods_id IN (" + accountR.GoodsIds + ")"
	} else {
		where = "acid =" + gconv.String(accountR.Id)
	}
	// 今日收入
	todayStart, todayEnd := gtime.TodayStamp()
	todayIncome, err := orm.Db.Where(where).Where("pay_state = 1 AND pay_time >=? AND pay_time <=?", todayStart, todayEnd).Sum(new(tables.Order), "pay_amount")
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	// 今日订单数
	todayOrderNum, err := orm.Db.Where(where).Where("pay_state = 1 AND pay_time >=? AND pay_time <=?", todayStart, todayEnd).Count(new(tables.Order))
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	// 昨日收入
	yesterdayStart, yesterdayEnd := gtime.YesterdayStamp()
	yesterdayIncome, err := orm.Db.Where(where).Where("pay_state = 1 AND pay_time >=? AND pay_time <=?", yesterdayStart, yesterdayEnd).Sum(new(tables.Order), "pay_amount")
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	//昨日订单数
	yesterdayOrderNum, err := orm.Db.Where(where).Where("pay_state = 1 AND pay_time >=? AND pay_time <=?", yesterdayStart, yesterdayEnd).Count(new(tables.Order))
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	data := gin.H{
		"today_income":        todayIncome,
		"today_order_num":     todayOrderNum,
		"yesterday_income":    yesterdayIncome,
		"yesterday_order_num": yesterdayOrderNum,
	}
	response.Success(c, "", data)
}

func (ctl *dashboardCtl) Everyday(c *gin.Context) {
	accountR, _ := service.Request.Account(c)
	t := time.Now()
	todayStart := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
	data := [](map[string]interface{}){}
	for n := 0; n < 30; n++ {
		currData := todayStart.AddDate(0, 0, -30+n)
		start := currData.Unix()
		end := currData.Unix() + 86400
		//判断子账户
		where := ""
		if accountR.Role == "goods" {
			where = "goods_id IN (" + accountR.GoodsIds + ")"
		} else {
			where = "acid =" + gconv.String(accountR.Id)
		}
		orderNum, err := orm.Db.Where(where).Where("pay_state = 1 AND pay_time >=? AND pay_time <=?", start, end).Count(new(tables.Order))
		if err != nil {
			response.Error(c, err.Error(), nil)
			return
		}
		data = append(data, gin.H{
			"x": gconv.String(currData.Day()) + "日",
			"y": orderNum,
		})
	}
	response.Success(c, "", data)
}

// 热门商品
func (ctl *dashboardCtl) HotGoods(c *gin.Context) {
	accountR, _ := service.Request.Account(c)
	list := make([]model.ReqGoods, 0)
	where := ""
	//判断子账户
	if accountR.Role == "goods" {
		where = "id IN (" + accountR.GoodsIds + ")"
	} else {
		where = "acid =" + gconv.String(accountR.Id)
	}
	err := orm.Db.Where(where).Desc("sales").Limit(5).Find(&list)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "", list)
}
